public class ZB {
    public static void main(String[] args) {
        int[] w = {10, 20, 30, 15};
        double[] v = {60, 100, 120, 45};
        double i = maxNum(w, v, 50);
        System.out.println("i = " + i);
    }

    /**
     * 背包问题
     * @param w 重量数组
     * @param v 价值数组
     * @param bage 背包载重
     * @return
     */
    public static double maxNum(int[] w, double[] v, int bage) {
        if(w == null || v == null || bage<=0 || w.length != v.length || w.length == 0) {
            return 0;
        }
        return pross(w, v, 0, bage);
    }

    /**
     * 当前下标的最优选择
     * @param w 重量数组
     * @param v 价值数字
     * @param index 当前下标
     * @param currBage 当前背包剩余载重
     * @return
     */
    public static double pross(int[] w, double[] v, int index, int currBage){
        if(currBage < 0){
            return -1;
        }
        if(index == w.length){
            return 0;
        }
        //不选择当前的物品
        double p1 = pross(w, v, index+1, currBage);

        //不选择当前的物品
        double p2 = 0;
        double pross = pross(w, v, index + 1, currBage - w[index]);
        if(pross != -1){
            p2 = pross + v[index];
        }

        return Math.max(p1, p2);
    }
}
